<#assign base=request.contextPath />
<!DOCTYPE html>
<div class="row">
	<div class="col-md-12 col-sm-12 col-xs-12">
		<div class="x_panel">
			<div class="x_title">
				<h2>角色编辑 <small></small></h2>
				<div class="clearfix"></div>
			</div>
			<div class="x_content">
				<br/>
				<form id="postForm" data-parsley-validate class="form-horizontal form-label-left">
					<input type="hidden" id="id" name="id" value="${((role_get.id)?c)!'' }">
		            <input type="hidden" name="createTime" value="${(role_get.createTime)!.now?string['yyyy-MM-dd HH:mm:ss']}">
		            <input type="hidden" name="updateTime" value="${.now?string['yyyy-MM-dd HH:mm:ss']}">
		            <input type="hidden" name="creator" value="${(role_get.creator)!admin.userName}">
                    <input type="hidden" id="menuIds" name="menuIds">
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">角色名称 <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="roleName" name="roleName" value="${(role_get.roleName)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">角色描述 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                        	<input type="text" id="roleDesc" name="roleDesc" value="${(role_get.roleDesc)!''}" class="form-control col-md-7 col-xs-12">
                        </div>
					</div>
					<div class="form-group">
						<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">角色权限 <span class="required"></span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
			                	<table class="table">
			                    	<tbody>
			                    		<#list allMenuList as obj>
			                            <tr>
			                            	<td width="20%;">
			                            	<lable><input type="checkbox" class="flat" id="l_1_${(obj.id)?c}" name="roleMenu" value="${(obj.id)?c}" <#if allMenuRole?seq_contains(obj.id)>checked="checked"</#if>>${obj.menuName}</lable></td>
			                                <td width="80%;">
			                                	<#list obj.list as obj2>
			                                	<lable><input type="checkbox" class="flat" id="l_2_${(obj2.id)?c}_${(obj.id)?c}" name="roleMenu" value="${(obj2.id)?c}" <#if allMenuRole?seq_contains(obj2.id)>checked="checked"</#if>>${obj2.menuName}&nbsp;</lable>
			                                	</#list>
			                                </td>
			                             </tr>
			                             </#list>
			                        </tbody>
			                     </table>
                        </div>
					</div>
					<div class="ln_solid"></div>
					<div class="form-group">
						<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
							<button type="button" class="btn btn-success" id="submit">保存</button>
							<button type="button" class="btn btn-primary" id="goBack">返回</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function() {
	//iCheck
	if ($("input.flat")[0]) {
		$(document).ready(function () {
	    	$('input.flat').iCheck({
	        	checkboxClass: 'icheckbox_flat-green',
	        	radioClass: 'iradio_flat-green'
	        });
	    });
	}
	// /iCheck
	$("input:checkbox[name='roleMenu']").on('ifChecked', function(event){
		var itemIdInfo = [];
		var itemId = $(this).attr("id");
		itemIdInfo = $(this).attr("id").split("_");//切割ID数组，下标：1权限层级 2当前ID 3父ID
		var lever = itemIdInfo[1];
		//选中一级菜单，不做处理
		//选中二级菜单，一级菜单一起选中
		if(lever=="2"){
			if($(this).is(":checked")){
				$('#l_1_'+itemIdInfo[3]).iCheck('uncheck');
				$('#l_1_'+itemIdInfo[3]).iCheck('check');
			}
		}
	});

	$("#goBack").click(function() {
		$("#main_content").load('${base}/gx/sysRole/list');	        
    });
	
	$("#submit").click(function() {
	
		var roleName = $.trim($("#roleName").val());
    	if(roleName==""){
    		swal("请输入角色名称");
            return;
    	}
		$("#menuIds").val("");
		var menuIds = $.trim($("#menuIds").val());
		$.each($('input:checkbox'),function(){
            if(this.checked){
            	menuIds += $(this).val()+",";
            }
        });
		if(menuIds == ""){
			swal("请勾选权限");
             return;
		}
		menuIds = menuIds.substr(0,menuIds.length-1);
		$("#menuIds").val(menuIds);
        var url = "${base}/gx/sysRole/save";
        $.post(url, $("#postForm").serialize(), function(data) {
            if (data.flag) {
            	swal(data.msg, "", "success");
                $("#main_content").load('${base}/gx/sysRole/list');
            }else{
            	swal(data.msg, "", "error");
            }
        });
    });
});
</script>